

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#include "common/utils.hpp"

// 层序遍历后，遍历到最右边时保存结果
std::vector<int> rightSideView(TreeNode* root) {
    std::vector<int> result;

    std::queue<TreeNode*> queue;
    if (root != nullptr) queue.push(root);

    while (!queue.empty())
    {
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            TreeNode* node = queue.front();
            queue.pop();
            if (node->left) queue.push(node->left);
            if (node->right) queue.push(node->right);
            if (i == size - 1) result.push_back(node->val);
        }
    }

    return result;
}

extern TreeNode* commonRoot;

int main() {
    std::vector<int> res = rightSideView(commonRoot);
    std::cout << "rightSideView: \n";
    printVector(res);
}